package com.example.musicwebcon.service;

import com.example.musicwebcon.model.dto.favlistDTO;
import com.example.musicwebcon.model.favlist;
import com.example.musicwebcon.repository.favlistRepository;
import com.example.musicwebcon.service.serviceInterface.IfavlistService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class favlistService implements IfavlistService {
    @Autowired
    favlistRepository favlistRepository;
    @Override
    public favlist add(favlistDTO favlist) {
        favlist songlist = new favlist();
        BeanUtils.copyProperties(favlist,songlist);
        return favlistRepository.save(songlist);
    }

    @Override
    public favlist edit(favlistDTO favlist) {
        favlist songlist = new favlist();
        BeanUtils.copyProperties(favlist,songlist);
        return favlistRepository.save(songlist);
    }

    @Override
    public favlist get(String songListId) {
        return favlistRepository.findById(songListId).orElseThrow(() -> {
            throw new IllegalArgumentException("歌曲不存在，参数异常！");
        });
    }

    @Override
    public Iterable<favlist> getALl() {
        return favlistRepository.findAll();
    }

    @Override
    public Iterable<favlist> getALlByuserId(String userId) {
        return favlistRepository.findByUserId(userId);
    }

    @Override
    public void delete(String songListId) {
        favlistRepository.deleteById(songListId);
    }
}
